package yuejian_07.yuejian_07.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.activity.MasInfoActivity;
import yuejian_07.yuejian_07.javabean.Master;




public class MasterListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public MasterListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Master entity = (Master) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.master_name.setText(entity.getMaster_name());
        viewHolder.master_hot.setText(entity.getMaster_hot()+ "");
        System.out.printf("master_name");
        String master_pic ="http://10.0.2.2/neu1_yuejian/Public/Admin/photo/"+entity.getMaster_pic();
        Picasso.with(mContext).load(master_pic).into(viewHolder.master_pic);
        if(viewHolder.master_pic.getDrawable()==null){
            viewHolder.master_pic.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("master_id",entity.getMaster_id());
                bundle.putString("master_name",entity.getMaster_name());
                bundle.putString("master_pic",entity.getMaster_pic());
                bundle.putInt("master_hot",entity.getMaster_hot());
                Intent intent=new Intent(mContext, MasInfoActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_master, parent, false);
        return new MasterListAdapter.ViewHolder(v);

    }

    //自定义的V
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView master_pic;
        TextView master_name,master_hot;

        public ViewHolder(View itemView) {
            super(itemView);
            master_name = (TextView) itemView.findViewById(R.id.Master_name);
            master_pic = (ImageView) itemView.findViewById(R.id.Masterlist_pic);
            master_hot = (TextView) itemView.findViewById(R.id.hot_count);
        }
    }

}
